package com.id.supaclean.widget;


import android.animation.ObjectAnimator;
import android.animation.ValueAnimator;
import android.content.Context;
import android.content.res.TypedArray;
import android.graphics.drawable.Drawable;
import android.util.AttributeSet;
import android.view.View;
import android.view.animation.LinearInterpolator;
import android.widget.ImageView;
import android.widget.LinearLayout;

import androidx.core.content.ContextCompat;

import com.id.supaclean.R;


public class MyRatingBar extends LinearLayout implements View.OnClickListener {
    private RatingChangeListener listener;
    private int starMax;
    private float starWidth;
    private float starHeight;
    private Drawable starEmpty;
    private Drawable starFill;
    private int rating;//点亮的星星数目
    private boolean isIndicator;//是否可以点击
    private Context context;

    public MyRatingBar setRatingChangeListener(RatingChangeListener listener) {
        this.listener = listener;
        return this;
    }

    public MyRatingBar setStarFill(int res) {
        this.starFill = ContextCompat.getDrawable(context, res);
        return this;
    }

    public MyRatingBar setStarEmpty(int res) {
        this.starEmpty = ContextCompat.getDrawable(context, res);
        return this;
    }

    public MyRatingBar setStarMax(int starMax) {
        this.starMax = starMax;
        return this;
    }

    public MyRatingBar setRating(int rating) {
        this.rating = rating > starMax ? starMax : rating;
        return this;
    }

    public void runAnim() {
        drawStar();
    }

    public void setStarWidth(float starWidth) {
        this.starWidth = starWidth;
    }

    public void setStarHeight(float starHeight) {
        this.starHeight = starHeight;
    }

    public MyRatingBar(Context context) {
        this(context, null);
    }

    public MyRatingBar(Context context, AttributeSet attrs) {
        this(context, attrs, 0);
    }

    public MyRatingBar(Context context, AttributeSet attrs, int defStyleAttr) {
        super(context, attrs, defStyleAttr);
        this.context = context;
        TypedArray ta = context.obtainStyledAttributes(attrs, R.styleable.MyRatingBar, defStyleAttr, 0);
        starEmpty = ta.getDrawable(R.styleable.MyRatingBar_starEmpty);
        starFill = ta.getDrawable(R.styleable.MyRatingBar_starFill);
        starWidth = ta.getDimension(R.styleable.MyRatingBar_star_width, 60);
        starHeight = ta.getDimension(R.styleable.MyRatingBar_star_height, 120);
        rating = ta.getInteger(R.styleable.MyRatingBar_rating, 0);
        starMax = ta.getInteger(R.styleable.MyRatingBar_starMax, 5);
        isIndicator = ta.getBoolean(R.styleable.MyRatingBar_starIndicator, true);
        ta.recycle();
        setOrientation(LinearLayout.HORIZONTAL);
        //防止使用者乱输点亮的星星数目
        rating = Math.min(rating, starMax);
        //绘制初始状态
        drawInit(context);
    }

    /**
     * 绘制初始状态
     */
    private void drawInit(Context context) {
        for (int i = 0; i < starMax; i++) {
            ImageView image = getImageView(context);
            if (i < rating) {
                image.setImageDrawable(starFill);
            } else {
                image.setImageDrawable(starEmpty);
            }
            image.setOnClickListener(this);
            addView(image);
        }
    }


    @Override
    public void onClick(View view) {
        if (!isIndicator) {
            return;
        }

        rating = indexOfChild(view) + 1;
        drawStar();
        if (listener != null) {
            listener.onRatingChange(rating);
        }
    }

    private ImageView getImageView(Context context) {
        ImageView image = new ImageView(context);
        LayoutParams para = new LayoutParams(Math.round(starWidth), Math.round(starHeight), 1);
        image.setLayoutParams(para);
        return image;
    }

    /**
     * 绘制点亮的星星
     */
    private void drawStar() {
        for (int i = 0; i < starMax; i++) {
            ImageView image = (ImageView) getChildAt(i);
            if (i < rating) {
                image.setImageDrawable(starFill);
                animClick(image);
            } else {
                image.setImageDrawable(starEmpty);
            }
        }
    }

    /**
     * 点击动画
     */
    private void animClick(View view) {
        ObjectAnimator alpha = ObjectAnimator.ofFloat(view, "alpha", 0.3f, 1);
        alpha.setDuration(200);//设置动画时间
        alpha.setInterpolator(new LinearInterpolator());//设置动画插入器
        alpha.setRepeatCount(0);//设置动画重复次数。Animation.INFINITE 表示重复多次
        alpha.setRepeatMode(ValueAnimator.RESTART);//RESTART表示从头开始，REVERSE表示从末尾倒播
        alpha.start();//启动动画。
    }

    public interface RatingChangeListener {
        void onRatingChange(int rating);
    }
}

